<?php
/**
	File: common.inc.php
	Author: Apple
	Update: 2009-9-22 16:27:51
*/
error_reporting(0);
set_magic_quotes_runtime(0);
$mtime = explode(' ', microtime());
$start_time = $mtime[0] + $mtime[1];

define('SYS_DEBUG', TRUE);
define('IN_SITE', TRUE);
define('SITE_ROOT', substr(dirname(__FILE__), 0, -7));
define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());
define('CACHE_PATH', SITE_ROOT.'data/cache/');
define('HTTP_REFERER', isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '');

if(defined('SYS_DEBUG' && SYS_DEBUG)) {
	error_reporting(E_ERROR | E_WARNING | E_PARSE);
}

if(PHP_VERSION < '4.1.0') {
	$_GET = &$HTTP_GET_VARS;
	$_POST = &$HTTP_POST_VARS;
	$_COOKIE = &$HTTP_COOKIE_VARS;
	$_SERVER = &$HTTP_SERVER_VARS;
	$_ENV = &$HTTP_ENV_VARS;
	$_FILES = &$HTTP_POST_FILES;
}

if (isset($_REQUEST['GLOBALS']) || isset($_FILES['GLOBALS'])) {
	exit('Request tainting attempted.');
}

require_once SITE_ROOT.'include/global.func.php';

getrobot();
if(defined('NOROBOT') && IS_ROBOT) {
	exit(header("HTTP/1.1 403 Forbidden"));
}

foreach(array('_COOKIE', '_POST', '_GET') as $_request) {
	foreach($$_request as $_key => $_value) {
		$_key{0} != '_' && $$_key = s_addslashes($_value);
	}
}

if (!MAGIC_QUOTES_GPC && $_FILES) {
	$_FILES = s_addslashes($_FILES);
}

require_once SITE_ROOT.'./config.inc.php';

$request_uri = request_uri();
if($urlxssattack && !empty($request_uri)) {
	$temp = urldecode($request_uri);
	if(strpos($temp, '<') !== false || strpos($temp, '"') !== false) {	exit('Request Bad url');	}
	unset($request_uri);
}

$_SCOOKIE = $_SSESSION = $_SCACHE = array();

$prelength = strlen($cookiepre);
foreach($_COOKIE as $key => $val) {
	if(substr($key, 0, $prelength) == $cookiepre) {
		$_SCOOKIE[(substr($key, $prelength))] = MAGIC_QUOTES_GPC ? $val : s_addslashes($val);
	}
}
unset($prelength, $_request, $_key, $_value);

$timestamp = time();

//开始攻击防御
//if($attackevasive) {
//	require_once SITE_ROOT.'./include/security.inc.php';
//}

$PHP_SELF = s_htmlspecialchars($_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME']);
$BASESCRIPT = basename($PHP_SELF);
if(!isset($forward) && str_exists(HTTP_REFERER, '?')) $forward = HTTP_REFERER;

$ip = ip();

if($gzipcompress && function_exists('ob_gzhandler') && !in_array(CURSCRIPT, array('attachment')) && !$inajax) {
	ob_start('ob_gzhandler');
} else {
	$gzipcompress = 0;
	ob_start();
}

//系统负载控制
if(!empty($loadctrl) && substr(PHP_OS, 0, 3) != 'WIN') {
	if($fp = @fopen('/proc/loadavg', 'r')) {
		list($loadaverage) = explode(' ', fread($fp, 6));
		fclose($fp);
		if($loadaverage > $loadctrl) {
			header("HTTP/1.0 503 Service Unavailable");
			include SITE_ROOT.'include/serverbusy.htm';
			exit();
		}
	}
}

$db = load('db_mysql.class.php');
$db->connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect, true, $dbcharset);
$dbuser = $dbpw = $pconnect = NULL;
include SITE_ROOT.'./data/cache/cache_settings.php';
@extract($_SCACHE['settings']);
if(!$cronnextrun || $cronnextrun <= $timestamp) {
	require_once SITE_ROOT.'./include/cron.func.php';
	runcron();
}


//define('FORMHASH', formhash());
//
require_once SITE_ROOT.'include/version.inc.php';

?>